package com.toe.user.webapi.controller;

import com.toe.common.exception.ToeServiceException;
import com.toe.common.restful.JsonResult;
import com.toe.common.restful.StateEnum;
import com.toe.pojo.dto.user.UserCodeDTO;
import com.toe.pojo.dto.user.UserDTO;
import com.toe.pojo.dto.user.UserPasswordDTO;
import com.toe.pojo.entity.user.UserUsertbl;
import com.toe.pojo.vo.user.UserUserVO;
import com.toe.user.service.UserUsertblService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import springfox.documentation.spring.web.json.Json;

import java.io.IOException;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author 陶涛
 * @since 2022-07-19
 */
@RestController
@RequestMapping("/webapi/user-usertbl")
@Api(tags = "用户模块")
public class UserUsertblController {
    @Autowired
    private UserUsertblService userService;
    /**
     * 获取用户详情信息
     * @return
     */
    @GetMapping("/get-user-info")
    @ApiOperation(value = "获取用户详细信息")
    public JsonResult<UserUserVO> getUserDetail() {
        UserUserVO userDetail = userService.getUserDetail();
        return JsonResult.ok(userDetail);
    }

    @PostMapping("/update-user-info")
    @ApiOperation(value = "更新用户信息")
    public JsonResult<String> updateUserDetail(@RequestBody UserDTO userDTO) {
        if (userService.updateUserDetail(userDTO))
        return JsonResult.ok("修改成功");
        else throw new ToeServiceException(StateEnum.ERR_SERVER_ERROR,"服务器忙,修改失败,请稍后再试!");
    }

    /**
     * 注册业务
     *
     * @param userDTO
     */
    @PostMapping("/reg")
    @ApiOperation(value = "注册")
    public JsonResult<String> reg(@RequestBody @Validated UserDTO userDTO) {
        userService.reg(userDTO);
        return JsonResult.ok("注册成功!");
    }

    @GetMapping("/send-code")
    @ApiOperation(value = "发送验证码")
    public JsonResult<String> sentCode(String phone) {
        userService.sentCode(phone);
        return JsonResult.ok("发送成功！");
    }
    @PostMapping("/check-code")
    @ApiOperation(value = "验证验证码")
    public JsonResult<Boolean> checkCode(@RequestBody UserCodeDTO codeDTO) {
        Boolean check = userService.check(codeDTO);
        return JsonResult.ok(check);
    }

    /**
     * 上传头像
     * @param avatar
     */
    @PostMapping("/update-avatar")
    @ApiOperation(value = "修改头像")
    public JsonResult<String> updateAvatar(@RequestPart("file")MultipartFile avatar) throws IOException {
        return JsonResult.ok(userService.updateAvatar(avatar));
    }

    /**
     * 修改密码
     */
    @PostMapping("/update-password")
    @ApiOperation(value = "修改密码")
    public JsonResult<Void> updatePassword(@RequestBody UserPasswordDTO passwordDTO) {
        userService.updatePassword(passwordDTO);
        return JsonResult.ok();
    }
}

